package com.rrd.hive.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    private static final SimpleDateFormat yyy_mm_dd_format = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat ymd_format = new SimpleDateFormat(
            "yyyy-MM-dd");


    /**
     * 比较date1 是否笔date2小
     * @Title: isBefore
     * @Description: TODO
     * @param @param date1
     * @param @param date2
     * @param @return
     * @return boolean
     * @throws
     */
    public static boolean isBefore(Date date1, Date date2) {
        return date1.before(date2);
    }

    public static String formateDateStr(Date date, String pattern) {
        String datestr = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            datestr = sdf.format(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("date==>" + date);
            e.printStackTrace();
        }
        return datestr;

    }

    public static String formateDateStr(Date date) {
        return ymd_format.format(date);

    }
    public static Date formateToDate(String datestr, String pattern) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            date = sdf.parse(datestr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            System.out.println("datestr=>" + datestr);
            e.printStackTrace();
        }
        return date;
    }
    public static String add_days(String date_str,int days){
        Date d1=formateToDate(date_str,"yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();;
        c1.setTime(d1);
        c1.add(Calendar.DAY_OF_MONTH,days);
        return formateDateStr(c1.getTime());
    }



    public static int date_diff(String s1,String s2){
    	Date d1=formateToDate(s1,"yyyy-MM-dd");
    	Date d2=formateToDate(s2,"yyyy-MM-dd");
    	 Calendar c1 = Calendar.getInstance();;
         Calendar c2 = Calendar.getInstance();
         c1.setTime(d1);
         c2.setTime(d2);
       //设置时间为0时
         c1.set(Calendar.HOUR_OF_DAY, 0);
         c1.set(Calendar.MINUTE, 0);
         c1.set(Calendar.SECOND, 0);
         c2.set(Calendar.HOUR_OF_DAY, 0);
         c2.set(Calendar.MINUTE, 0);
         c2.set(Calendar.SECOND, 0);
         int days = ((int)(c1.getTime().getTime()/1000)-(int)(c2.getTime().getTime()/1000))/3600/24;
    	return days;
    }
   
    public static void main(String[] args) throws ParseException {
        System.out.println(date_diff("2021-04-30","2021-01-30"));
    }
}
